package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {
    @Autowired
    private ConsumptionMapper consumptionMapper;

    @Autowired
    private IAppointmentService appointmentService;

    @Override
    @Transactional
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void insert(Consumption consumption) {
        // 设置结算单状态
        consumption.setStatus(Consumption.STATUS_TYPE_CONSUME);
        // 设置流水单号 日期+随机数
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        int random = new Random().nextInt(900000) + 100000;
        consumption.setCno(date + random);
        // 设置创建时间
        consumption.setCreateTime(new Date());
        // 结算人  若为空,是因为没有登录
        Employee employee = UserContext.getEmployee();
        consumption.setCreateUserId(employee.getId());
        consumptionMapper.insert(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    @Transactional
    public void update(Consumption consumption) {
        // 设置结算单状态 => 待结算  结算单已经存在,有些就不需要更新,比如 单号,结算人,结算时间,关联预约单号,
        consumption.setStatus(Consumption.STATUS_TYPE_CONSUME);
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public PageInfo<Consumption> query(ConsumptionQueryObject qo) {
        // 使用分页插件，传入当前页，每页显示数量
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "c.create_time desc");
        // 返回查询一页展示的数据
        return new PageInfo<>(consumptionMapper.selectForList(qo));
    }

    // 有预约,添加结算单
    @Override
    public Long save(Long aid) {
        // 设置预约单状态  => 消费中
        appointmentService.updateStatus(aid, Appointment.STATUS_TYPE_CONSUME);
        // 创建一个结算单对象
        Consumption consumption = new Consumption();
        // 获得预约单对象 将预约单里的信息 传给 结算单
        Appointment appointment = appointmentService.get(aid);
        // 设置备注
        consumption.setInfo(appointment.getInfo());
        // 设置客户名称和电话
        consumption.setCustomerName(appointment.getContactName());
        consumption.setCustomerTel(appointment.getContactTel());
        // 关联预约单  预约单的单号
        consumption.setAppointmentAno(appointment.getAno());
        // 业务类型
        consumption.setBusiness(appointment.getBusiness());
        // 设置结算单 状态
        consumption.setStatus(Consumption.STATUS_TYPE_CONSUME);

        // 设置流水单号 日期+随机数
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        int random = new Random().nextInt(900000) + 100000;
        consumption.setCno(date + random);
        // 设置创建时间
        consumption.setCreateTime(new Date());
        // 结算人 若为空,是因为没有登录
        Employee employee = UserContext.getEmployee();
        consumption.setCreateUserId(employee.getId());
        // 保存结算单
        consumptionMapper.insert(consumption);
        // 插入数据之后能立刻拿到这条数据的id ,mapper那里设置了主键自增
        return consumption.getId();
    }

    // 结算按钮
    @Override
    public Long consume(Consumption consumption) {
        // 设置结算单状态 => 待审核
        consumption.setStatus(Consumption.STATUS_TYPE_AUDIT);
        // 设置结算时间
        consumption.setPayTime(new Date());
        // 设置结算人 通过session拿到
        consumption.setEmployee(UserContext.getEmployee());
        consumptionMapper.updateConsume(consumption);
        return consumption.getId();
    }

    // 审核按钮
    @Override
    public Long audit(Consumption consumption) {
        // 设置结算单状态 =>
        if (consumption.getStatus() == 2) {
            consumption.setStatus(Consumption.STATUS_TYPE_FINISH); // 归档
        } else {
            consumption.setStatus(Consumption.STATUS_TYPE_GAILURE); // 坏账
        }
        // 设置审核人
        consumption.setAuditor(UserContext.getEmployee());
        // 设置审核时间
        consumption.setAuditTime(new Date());
        consumptionMapper.updateAudit(consumption);
        return consumption.getId();
    }

}
